package com.cskaoyan;

import com.mysql.jdbc.Driver;

import java.sql.*;

/**
 * 第一次去连接数据库
 * 创建日期: 2022/02/23 10:13
 *
 * @author ciggar
 */
public class Main4Query {

    public static void main(String[] args) throws SQLException {

        // 1. 注册驱动
        DriverManager.registerDriver(new Driver());

        // 2. 建立连接，获取连接对象
        // useSSL=false 关闭SSL协议的认证，为了消除警告
        // characterEncoding=utf8 指定客户端与服务器连接的字符集为utf8
        String url = "jdbc:mysql://localhost:3306/37th?useSSL=false&characterEncoding=utf8";
        String username = "root";
        String password = "123456";

        Connection connection = DriverManager.getConnection(url,username,password);

        // 3. 获取statement对象 statement对象主要是用来去帮助我们发送SQL语句的
        Statement statement = connection.createStatement();

        // 4. 发送请求
        ResultSet resultSet = statement.executeQuery("select * from clazz");


        // 5. 获取并解析SQL语句的执行结果
        while (resultSet.next()) {

            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");

            System.out.println("id:" + id +", name:" + name);

        }


        // 6. 释放资源
        resultSet.close();
        statement.close();
        connection.close();

    }
}
